<?php
require_once 'connect.php';

if (isset($_POST) && isset($_POST['username']) && isset($_POST['password']) && isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email'])) {
	$username = $_POST['username'];
	$password = $_POST['password'];
	$firstName = $_POST['first_name'];
	$lastName = $_POST['last_name'];
	$email = $_POST['email'];

	if (!isValid($username)) {
		//echo 'Invalid username: Please use 6 - 20 alphanumeric characters.';
		echo $username;
	} else if (!isValid($password)) {
		echo 'Invalid password: Please use 6 - 20 alphanumeric characters.';
	} else {
		// create the account
		connect();
		if (createAccount($firstName, $lastName, $email, $username , $password )) {
			echo 'true';
		} else {
			echo 'Please choose another username.';
		}
	}
} else {
	echo 'Unable to create account.';
}

function isValid($s) {
	return preg_match('/^[A-Za-z][A-Za-z0-9]{5,19}/', $s);
}
?>